define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'prize/prize/index' + location.search,
                    add_url: 'prize/prize/add',
                    edit_url: 'prize/prize/edit',
                    del_url: 'prize/prize/del',
                    multi_url: 'prize/prize/multi',
                    table: 'prize',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'top_switch desc,weigh asc',
                searchFormVisible:true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'),operate:'LIKE'},
                        {field: 'brief', title: __('Brief'), operate: false},
                        {field: 'goods.name', title: __('Goods_id'),operate:'LIKE'},
                        {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'total', title: __('Total'), operate: false},
                        {field: 'price', title: __('Price'), operate:'BETWEEN'},
                        {field: 'num', title: __('Num'), operate: false},
                        {field: 'starttime', title: __('Starttime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'endtime', title: __('Endtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'opentime', title: __('Opentime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'switch', title: __('Switch'), searchList: {"1":__('Yes'),"0":__('No')}, table: table, formatter: Table.api.formatter.toggle},
                        {field: 'top_switch', title: __('Top_switch'), searchList: {"1":__('Yes'),"0":__('No')}, table: table, formatter: Table.api.formatter.toggle},
                        {field: 'is_member', title: __('Is_member'), searchList: {"1":"会员限定","0":"非限定"}},
                        {field: 'is_level', title: __('Is_level'), searchList: {"1":"适用","0":"不适用"},visible:false},
                        {field: 'weigh', title: __('Weigh'), operate: false},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            // 显示开奖任务队列
            $(document).on("click", ".btn-queueList", function () {
                //在table外不可以使用添加.btn-change的方法
                //只能自己调用Table.api.multi实现
                //如果操作全部则ids可以置为空
                //var ids = Table.api.selectedids(table);
                //Table.api.multi("changestatus", ids.join(","), table, this);

                Fast.api.open('prize/prize/queueList','待开奖任务队列',{area:['1000px', '600px']});
            })
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        queuelist: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'prize/prize/queueList' + location.search,
                    table: 'prize',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                commonSearch: false,
                search:false,
                onlyInfoPagination: true,
                pagination: true,

                columns: [
                    [
                        {field: 'id', title: 'ID',operate: false},
                        {field: 'job', title: '执行类',operate: false},
                        {field: 'data.id', title: '抽奖ID', operate: false},
                        {field: 'data.name', title: '抽奖名称', operate: false},
                        {field: 'data.opentime', title: '开奖时间', operate: false},
                        {field: 'exec_time', title: '队列执行时间', operate: false},
                        {field: 'attempts', title: '次数', operate: false},
                        //{field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            // 一键生成定时开奖任务
            $(document).on("click", ".btn-openPrize", function () {
                //在table外不可以使用添加.btn-change的方法
                //只能自己调用Table.api.multi实现
                //如果操作全部则ids可以置为空
                //var ids = Table.api.selectedids(table);
                //Table.api.multi("changestatus", ids.join(","), table, this);

                layer.confirm('是否重新生成开奖任务？', {
                    btn: ['确定', '取消'],
                    title: '提示',
                }, function(index) {
                    layer.close(index);
                    Fast.api.ajax({
                        url: 'prize/prize/openPrize',
                    }, function (data, ret) {

                        if (ret.data){
                            var successList = ret.data.successList;
                            var errorList = ret.data.errorList;
                            var content = '';
                            $.each(errorList,function(index,value){
                                content += 'ID:' + value.id + '|' + value.name + '</br>';
                                //console.log(index+"..."+value.id);
                            });

                            content = content ? content : '无';

                            var head = ret.msg + ' ' + ret.data.total + '条';
                            var message = head + '</br>' + '错误记录:</br>' + content;
                            table.bootstrapTable('refresh', {});
                            Layer.alert(message);
                        }

                    }, function (data, ret) {
                        Layer.alert(ret.msg);
                        return false;
                    });

                }, function() {});
            })
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});